package com.yisu.auction.manager.website.controller.activityInfo;

import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.ewf.entity.SecKillActivity;
import com.yisu.auction.manager.core.service.ISecKillActivityService;
import com.yisu.auction.manager.domain.request.baseInfoPackage.SecKillActivityRequest;
import com.yisu.auction.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: 赵杰
 * Business Description：秒杀业务活动
 * createTime：2017/8/23
 */
@RequestMapping(value = "/secKill")
@Controller
public class SecKillActivityController extends BaseController {
    @Resource
    private ISecKillActivityService iSecKillActivityService;

    /**
     * author: 赵杰
     * Business Description：获取商品信息首页
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, HttpServletRequest request, SecKillActivityRequest secKillActivityRequest) throws Exception {
        try {
            if (secKillActivityRequest.getKeyword() != null) {
                secKillActivityRequest.setKeyword(URLDecoder.decode(secKillActivityRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<SecKillActivity> secKillActivityByPage = iSecKillActivityService.getSecKillActivityByPage(secKillActivityRequest);
            model.addAttribute("SecKillActivityVos", secKillActivityByPage.getRows());
            model.addAttribute("currentPage", secKillActivityRequest.getPage());
            model.addAttribute("totalPages", (secKillActivityRequest.getTotalRecords() / secKillActivityRequest.getRows()) + 1);
            model.addAttribute("keyword", secKillActivityRequest.getKeyword());
        } catch (Exception e) {
            logger.error("获取商品信息首页异常：" + e.getMessage());
        }
        return "/baseInfo/secKill/index";
    }


    /**
     * author: 赵杰
     * Business Description：商品档案编辑操作
     *
     * @param secKillActivityRequest 商品档案基本信息
     * @return 状态
     */
    @RequestMapping(value = "/joinSecKillAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult joinSecKillAction(HttpServletRequest request, SecKillActivityRequest secKillActivityRequest) {
        try {
            if (iSecKillActivityService.joinSecKillByGoodsId(secKillActivityRequest)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }


    /**
     * author: 赵杰
     * Business Description：商品档案编辑操作
     *
     * @param secKillActivityRequest 商品档案基本信息
     * @return 状态
     */
    @RequestMapping(value = "/modifySecKillInfoAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifySecKillInfoAction(HttpServletRequest request, SecKillActivityRequest secKillActivityRequest) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSSSS");//临时编号
            SecKillActivity secKillActivity = iSecKillActivityService.getSecKillActivityById(secKillActivityRequest);
            if (secKillActivity != null && secKillActivity.getStatus() == 1) {
                return new JsonResult(ResponseCode.FAILURE.code, "已经开奖的不能修改！");
            } else {
                SecKillActivity updateModel = new SecKillActivity();
                updateModel.setId(secKillActivity.getId());
                updateModel.setStartTime(simpleDateFormat.parse(secKillActivityRequest.getStartTime()));
                updateModel.setEndTime(simpleDateFormat.parse(secKillActivityRequest.getEndTime()));
                if (iSecKillActivityService.updateSecKillActivity(updateModel)) {
                    return new JsonResult(ResponseCode.SUCCESS.code, "修改成功！");
                }
                return new JsonResult(ResponseCode.SUCCESS.code, "修改失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

}
